package com.huajx.tuopuda.client.udp;

import com.huajx.core.Hboot;
import com.huajx.core.udp.UDPClient;
import com.huajx.core.udp.annonation.MinaClient;
import com.huajx.core.udp.config.UDPClientConfig;
import com.huajx.tuopuda.client.udp.codec.SetDatetime;
import com.huajx.tuopuda.client.udp.config.SettingConfig;
import com.huajx.tuopuda.client.udp.handler.AppendAuthHandler;
import com.huajx.tuopuda.client.udp.handler.ReadRunStatusHandler;
import com.huajx.tuopuda.client.udp.handler.SearchNetMachineHandler;
import com.huajx.tuopuda.client.udp.handler.SetDatetimeHandler;
import com.huajx.tuopuda.client.udp.handler.TcpIpHandler;
import com.huajx.tuopuda.client.udp.model.Drive;
import com.huajx.util.StrUtil;
import com.huajx.util.convert.Convert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-26 13:58
 */
@MinaClient(port = 60000, name = "tuopuda")
public class TuopudaClient implements UDPClient {
    private final static Logger logger = LoggerFactory.getLogger(TuopudaClient.class);

    
    public static final int DEFAULT_PORT = 60000;
    public static final String DEFAULT_IP = "192.168.1.3";

    private boolean isRunning;

    private int port = DEFAULT_PORT;
    private String ip;
    private int sn;
    private String name;
    private String remark;
    
    public static Map<String,Integer> map=new ConcurrentHashMap<>();
    
    public static int getRecordCount(int sn){
    	return Convert.toInt(map.get(String.valueOf(sn)),0);
    }
    
    public static void  setRecordCount(int sn,int record){
    	map.put(String.valueOf(sn),record);
    }

    private SettingConfig settingConfig = Hboot.config(SettingConfig.class);

    @Override
    public void start() {
        new Thread(() -> {
            start(ip, port, sn);
        }).start();
    }

    public void start(String ip, int port, int sn) {
        logger.info("{}:{} 数据采集正在启中......",ip, sn);
        if(isRunning) {
            return;
        }
        if(onUpload(sn)) {
            SearchNetMachineHandler.me().send(ip, port, sn);
            AppendAuthHandler.me().send(ip, port, sn);
        }
        //重置机器版时间
        SetDatetimeHandler.me().send(ip, port, sn);
   /*     Drive drive=new Drive();
        drive.setGateway("192.168.0.1");
        drive.setMask("255.255.255.0");
        drive.setMac("00-57-47-02-16-5C");
        drive.setIp("192.168.0.4");
        drive.setSn(38144);
        try {
			TcpIpHandler.me().send(ip, port, sn,drive);
		} catch (TuopudaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		try {
			int record=ReadWriteRecord.read(sn);//读取上次读取的record
			map.put(String.valueOf(sn), record);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        while (true) {
            try {
                ReadRunStatusHandler.me().send(ip, port, sn);
                if(ReadRunStatusHandler.onOff){
                	 Thread.sleep(25000);
                }else{
                	 Thread.sleep(settingConfig.getThreadSleep());
                }
               
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean onUpload(int sn) {
        File hasUpload = new File(sn + "-on.ini");
        if(hasUpload.exists()) {
            return false;
        }
        try {
            return hasUpload.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void config(UDPClientConfig config) {
        int port = config.getPort();
        this.port = port <= 80 ? DEFAULT_PORT : port;
        this.name = config.getName();
        this.remark = config.getRemark();
        this.sn = config.getSn();
        String ip = config.getIp();
        this.ip = StrUtil.isBlank(ip) ? DEFAULT_IP : ip;
    }
}
